#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define BUF_SIZE 4096

static char buf[BUF_SIZE];

int main(int argc, char **argv)
{
    int rfd, wfd;
    size_t size;

    rfd = open(argv[1], O_RDONLY);
    if(0 > rfd){
        perror("open error");
        exit(-1);
    }

    wfd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0664);
    if(0 > wfd){
        perror("open error");
        goto err;
    }

    while(0 < (size = read(rfd, buf, BUF_SIZE)))
        write(wfd, buf, size);

    fsync(wfd);

    err:
        close(rfd);
        exit(-1);

    close(rfd);
    close(wfd);
    exit(0);
}